< previous page page_387 next page >

Page 387
Because both of the old values of the point's X and Y coordinates are used in both of these equations, you must be careful when using the equations in Visual Basic. You might try to execute the statements in order like this:
e10a6a0a59f035e870d90cca05b85fce.gif
X = X * Cos(theta) - Y * Sin(theta)
Y = X * Sin(theta) + Y * Cos(theta)
Unfortunately the first statement changes the value of the variable X. The original value of X is not available for use in the second statement, so the new value for Y is wrong. The following three lines show one way you can rotate a point correctly in Visual Basic.
e10a6a0a59f035e870d90cca05b85fce.gif
new_x = X * Cos(theta) - Y * Sin(theta)
Y = X * Sin(theta) + Y * Cos(theta)
X = new_x
Figure 12.12 shows a small picture before and after rotation through an angle of 30 degrees.
The Mathematics of Rotation
This section explains why the rotation equations described in the previous section work. If your trigonometry is a little rusty, feel free to skip this section and take the equations on faith.
To see why the rotation equations work, look at Figure 12.13. The point (x1, y1) is distance R from the origin. Using simple trigonometry, you know that:
e10a6a0a59f035e870d90cca05b85fce.gif
x1 = R * Cos(f)
y
1 = R * Sin(f)
The point (x2, y2) is the same point rotated by an angle of q. Because it is also distance R from the origin, its coordinates are given by:
e10a6a0a59f035e870d90cca05b85fce.gif
x2 = R * Cos(f + q)
y
2 = R * Sin(f + q)
0387-01.gif
Figure 12.12
An object rotated by 30 degrees.

 
< previous page page_387 next page >